package com.ruoyi.web.controller.oto;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.MemberUser;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.oto.domain.MemberUserRole;
import com.ruoyi.oto.domain.dto.MembUserQuryDTO;
import com.ruoyi.oto.domain.dto.ReplaceUserDTO;
import com.ruoyi.oto.domain.dto.SetBranchUserDTO;
import com.ruoyi.oto.service.MemberUserRoleService;
import com.ruoyi.oto.service.MemberUserService;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 后台-后台会员相关
 */
@RestController
@RequestMapping("/adm/member")
public class MemberController extends BaseController{

    @Autowired
    private MemberUserRoleService memberUserRoleService;

    @Autowired
    private MemberUserService memberUserService;

    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 获取会员类型列表
     * @return
     */
    @GetMapping("listMemberType")
    public AjaxResult<List<MemberUserRole>> listMemberType() {
        List<MemberUserRole> list = memberUserRoleService.list();
        return AjaxResult.success(list);
    }

    /**
     * 获取后台所有的部门
     * @return
     */
    @GetMapping("listBranch")
    public AjaxResult<List<SysDept>> listBranch() {
        SysDept sysDept = new SysDept();
        sysDept.setParentId(101L);
        List<SysDept> list = sysDeptService.selectDeptList(sysDept);
        return AjaxResult.success(list);
    }

    /**
     * 后台分页查询会员列表
     * @param membUserQuryDTO
     * @return
     */
    @PostMapping("listMember")
    public AjaxResult<IPage<MemberUser>> pageMembUser(@RequestBody MembUserQuryDTO membUserQuryDTO) {
        MemberUser memberUser = new MemberUser();
        IPage<MemberUser> page = new Page<>(membUserQuryDTO.getCurrent(), membUserQuryDTO.getSize());
        page =  memberUser.selectPage(page,new LambdaQueryWrapper<MemberUser>().eq(membUserQuryDTO.getRoleId()!=null,MemberUser::getRoleId,membUserQuryDTO.getRoleId())
                .and(StrUtil.isNotEmpty(membUserQuryDTO.getSearchParam()), memberUserLambdaQueryWrapper -> memberUserLambdaQueryWrapper.like(MemberUser::getUsername, membUserQuryDTO.getSearchParam()).or()
                                                                                                                    .like(MemberUser::getNickName, membUserQuryDTO.getSearchParam()).or()
                                                                                                                    .like(MemberUser::getPhone, membUserQuryDTO.getSearchParam())));
        return AjaxResult.success(page);
    }

    /**
     * 设置会员的为联企人员
     * @return
     */
    @PostMapping("setMembBranch")
    public AjaxResult<Boolean> setBranchUser(@Validated  @RequestBody SetBranchUserDTO setBranchUserDTO) {
        memberUserService.setBranchUser(setBranchUserDTO,this.getLoginUser());
        return AjaxResult.success();
    }


    /**
     * 替换其他账号
     * 主要功能是：替换到其他联企人员和企业人员
     * @return
     */
    @PostMapping("replaceToOther")
    public AjaxResult<Boolean> replaceToOther(@Validated @RequestBody ReplaceUserDTO replaceUserDTO) {
        memberUserService.replaceToOther(replaceUserDTO);
        return AjaxResult.success();
    }
}
